<template>
<div>
	<a-modal
		:title="this.title"
		:visible="this.visible"
		:width="660"
   		@cancel="cancel"
   		@ok="ok"
	>
	<a-tabs type="card"  @change="callback" >
      <a-tab-pane key="1" tab="生产任务" v-if="this.demandClassify==='生产任务'" force-render>
        		<a-form
			    :form="form1"
			    v-bind="formItemLayout"
			  >
			  <a-form-item label="需求名称">
				      <a-input :value="this.demandName" readOnly
				      />
			  </a-form-item>
        <a-form-item label="需求截止日期">
              <a-input :value="this.demandYear" readOnly
         />
        </a-form-item>
			  <a-form-item label="任务名称">
				      <a-input
 						v-decorator="this.validator.name"
				      />
			  </a-form-item>
			   <a-form-item label="生产数据版本">
			  		 <a-date-picker
					v-decorator="this.validator.demandyear"
				      />
			  </a-form-item>
<!-- 			  <a-form-item label="生产任务单">
			      <a-upload
			        name="file"
			        :action="fileAction"
			        @change="handleUploadChange"
			         :showUploadList="false"
			         :data="uploadParam"
			        v-decorator="this.validator.upload"
			         >
			        <a-button> <a-icon type="upload" /> 选择文件 </a-button>
			      </a-upload>
			    </a-form-item> -->
			      <a-form-item label="范围">
			     <a-tree-select
				    show-search
				    style="width: 100%"
				    :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
				    allow-clear
				    tree-checkable
				    tree-default-expand-all
				    :tree-data="this.treeData"
				    v-decorator="this.validator.upload"
				     @change="onChange"
				  >
				  </a-tree-select>
				  </a-form-item>
				 <a-form-item label="流程编排">
				          <a-button type="primary" @click="flowArrange">
				           流程编排
				          </a-button>
				  </a-form-item>
			  <a-form-item label="任务时间">
				      <a-range-picker
 						v-decorator="this.validator.fronttime"
				      />
			  </a-form-item>

        </a-form>
      </a-tab-pane>
      <a-tab-pane key="2" tab="评估任务" v-if="this.demandClassify!=='生产任务'"  force-render>
                 <a-form
			    :form="form2"
			    v-bind="formItemLayout"
			  >
			  <a-form-item label="需求名称">
				      <a-input :value="this.demandName" readOnly
				      />
			  </a-form-item>
			  <a-form-item label="任务名称">
				      <a-input
 						v-decorator="this.validator1.name"
				       />
			  </a-form-item>
 				 <a-form-item label="评估任务类型">
				      <a-select
				       @change="handleChange"
					  v-decorator="this.validator1.taskClassify"
				       >
				        <a-select-option value="数据">
				         数据
				        </a-select-option>
				        <a-select-option value="应用">
				          应用
				        </a-select-option>
				       <a-select-option value="服务">
				          服务
				        </a-select-option>
				      </a-select>
				    </a-form-item>
				<a-form-item label="数据"  v-if="this.taskType==='数据'">
			      <a-upload
			        name="file"
              :headers='headers'
			        :action="estimateFileAction"
			        @change="handleUploadChange"
			         :showUploadList="false"
			         :data="uploadParam"
			         v-decorator="this.validator1.uploadData"
			      >
			        <a-button> <a-icon type="upload" /> 选择文件 </a-button>
			      </a-upload>
			    </a-form-item>
			    <a-form-item label="应用" v-if="this.taskType==='应用'">
			      <a-upload
			        name="file"
              :headers='headers'
			        :action="estimateFileAction"
			        @change="handleUploadChange"
			         :showUploadList="false"
			         :data="uploadParam"
			        v-decorator="this.validator1.uploadApplication"
			      >
			        <a-button> <a-icon type="upload" /> 选择文件 </a-button>
			      </a-upload>
			    </a-form-item>
				 <a-form-item label="服务" v-if="this.taskType==='服务'">
				      <a-select v-decorator="this.validator1.taskType">
					<a-select-option v-for="(item, key) of service"
							:value="item"
							:key='item'
					>{{item}}</a-select-option>
				      </a-select>
				    </a-form-item>

          <a-form-item label="任务时间">
            <a-range-picker v-decorator="this.validator1.frontyear"/>
          </a-form-item>

        </a-form>
      </a-tab-pane>
    </a-tabs>
  </a-modal>
      <modal
      ref="flowModalForm"
      :flowTitle="modalFlowTitle"
      :flowVisible="modalFlowVisible"
      @cancel="handleCancel"
      @ok="handleOk"
    >
    </modal>
    </div>
</template>

<script>
  import aModal from "@/common/modal"
  import modal from "./flowModal"
  import {
    ADDESTIMATETASK,
    ADDPRODUCTTASK,
    GETALLSERVICE,
    REGIONTREE,
    UPLOADESTIMATE,
    UPLOADPRODUCT,
    GETFLOWDATA
  } from '@/../static/js/apis.js'
  import {
    estimateTaskForm1,
    estimateTaskForm2,
    estimateTaskForm3,
    productTaskForm
  } from "@/../static/js/formValidators.js"
  import global from "@/../static/global.vue"
export default{
	inject:['reload'],                                 //注入App里的reload方法
	name: "taskModal",
	props: ['title', 'visible','demandName','demandId','demandYear','demandClassify','tabType','flowTitle','flowVisible'],
	components: {
		aModal,
		modal
	},
	data () {
		return {
			formItemLayout: {
		      labelCol: { span: 6 },
		      wrapperCol: { span: 14 },
		    },
		    taskType:'数据',
		    validator: productTaskForm,
		    validator1: estimateTaskForm1,
		    fileAction: UPLOADPRODUCT.url,   //导入文件提交路径
		    estimateFileAction:UPLOADESTIMATE.url,
		    service: [],
		    userid:'',
    		    uploadParam:{},
		    treeData:{},
		    modalFlowTitle:'流程编排',
		    modalFlowVisible:false,
		    area:[]
		}
	},
	 beforeCreate() {
	    this.form1 = this.$form.createForm(this, { name: 'productTaskForm' });
	    this.form2 = this.$form.createForm(this, { name: 'estimateTaskForm' });
	  },
	methods: {
      handleOk (e) {
	    this.modalFlowVisible = false
      },
      handleCancel(e) {
        this.modalFlowVisible = false
      },
      flowArrange(){
      	let self=this
      	if(self.area.length==0){
      		self.$message.error("请选择范围");
      	}else{
      		 localStorage.setItem("spinning", true)
      		 GETFLOWDATA(self.area,function(res){
      		 	localStorage.setItem("connlist", JSON.stringify(res.obj.connlist));
      		 	localStorage.setItem("list", JSON.stringify(res.obj.list1));
			 	self.modalFlowVisible = true
		      })

      	}

      },
	callback(key) {
	  this.tabType = key;
	 },
	onChange(value, label, extra) {
      this.area=value
     },
	getStaticData () {
  		let self=this
		 GETALLSERVICE('',function(res){
		 	self.service = res
	      })
		 REGIONTREE('',function(res){
		 	console.log(res)
		 	self.treeData = res.obj
	      })
		  self.userid=parseInt(localStorage.getItem("userid"))
		  self.uploadParam={
		  	 userid:self.userid
		  }


  	},
	handleUploadChange(info) {
	      if (info.file.status === 'done') {
	        this.$message.success(`${info.file.name} 上传成功`);
	      } else if (info.file.status === 'error') {
	        this.$message.error(`${info.file.name} 上传失败`);
	      }
	    },
   	 ok(e) {
	    	let self=this;
	     e.preventDefault();
	    	if(self.tabType==='1'){
	    	 self.form1.validateFields((err, values) => {
	        if (!err) {
                if(localStorage.getItem("flowJson")=='{}'){
                 	 this.$message.warning('请编排流程!');
                 	 return false
                }
	        	values.fronttime=[values.fronttime[0].format('YYYY-MM-DD'), values.fronttime[1].format('YYYY-MM-DD')]
	        	values.demanddata = values.demanddata.format('YYYY-MM-DD')
	        	values.demandId=self.demandId
	        	values.userid=self.userid
	          values.flow=JSON.parse(localStorage.getItem("flowJson"))
	          ADDPRODUCTTASK(values,function(res){
		          	if(res.code=='200'){
		          		self.$message.success(res.message);
				 	self.$emit("cancel", e)
				 	localStorage.setItem("activeIndex", '1');
					self.$router.push({path: '/taskManagement',query:{system:'业务管理'}})
					self.$store.commit('changeFirstNavSelected', '2')
		          	}else{
		          		self.$message.error(res.message);
		          	}

              })
            }
          });
        } else {
          if (self.taskType === "数据") {
            self.validator1 = estimateTaskForm1
          } else if (self.taskType === "应用") {
            self.validator1 = estimateTaskForm2
          } else if (self.taskType === "服务") {
            self.validator1 = estimateTaskForm3
          }
          self.form2.validateFields((err, values) => {
            if (!err) {
              values.frontyear = [values.frontyear[0].format('YYYY-MM-DD'), values.frontyear[1].format('YYYY-MM-DD')]
              values.demandId = self.demandId
              values.userid = self.userid
              ADDESTIMATETASK(values, function (res) {
                if (res.code == '200') {
                  self.$message.success(res.message);
                  self.form2.resetFields();
                  self.$emit("cancel", e)
                  localStorage.setItem("activeIndex", '2');
                  self.$router.push({path: '/taskManagement', query: {system: '业务管理'}})
                  self.$store.commit('changeFirstNavSelected', '2')
                } else {
                  self.$message.error(res.message);
                }
              })
            }
          });
        }


      },
      /*关闭modal*/
      cancel(e) {
        this.form1.resetFields();
        this.form2.resetFields();
        this.$emit("cancel", e)
      },
      handleChange(value) {
        let self = this
        self.taskType = value
        if (self.taskType === "数据") {
          self.validator1 = estimateTaskForm1
        } else if (self.taskType === "应用") {
          self.validator1 = estimateTaskForm2
        } else if (self.taskType === "服务") {
          self.validator1 = estimateTaskForm3
        }
      }

    },
  computed: {
    headers () {
      return {
        "token" : localStorage.getItem("token")
      }
    }
  },
    created() {
      this.getStaticData()
       var jsonData={}
       jsonData=JSON.stringify(jsonData)
      localStorage.setItem("flowJson", jsonData)
    },

  }
</script>

<style scoped>

</style>
